package com.example.hospital.mapper;

import com.example.hospital.domain.entity.ExaminationIndicator;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExaminationIndicatorMapper {
    @Insert("INSERT INTO examination_indicator (indicator_code, indicator_name, category_id, unit, lower_limit, upper_limit, price, description, specimen_type) " +
            "VALUES (#{indicatorCode}, #{indicatorName}, #{categoryId}, #{unit}, #{lowerLimit}, #{upperLimit}, #{price}, #{description}, #{specimenType})")
    @Options(useGeneratedKeys = true, keyProperty = "indicatorId")
    int insert(ExaminationIndicator indicator);

    @Update("UPDATE examination_indicator SET indicator_code=#{indicatorCode}, indicator_name=#{indicatorName}, category_id=#{categoryId}, unit=#{unit}, lower_limit=#{lowerLimit}, upper_limit=#{upperLimit}, price=#{price}, description=#{description}, specimen_type=#{specimenType} WHERE indicator_id=#{indicatorId}")
    int update(ExaminationIndicator indicator);

    @Delete("DELETE FROM examination_indicator WHERE indicator_id=#{indicatorId}")
    int delete(@Param("indicatorId") Long indicatorId);

    @Select("SELECT * FROM examination_indicator WHERE indicator_id=#{indicatorId}")
    ExaminationIndicator findById(@Param("indicatorId") Long indicatorId);

    @Select({
            "<script>",
            "SELECT * FROM examination_indicator",
            "<where>",
            "  <if test=\"indicatorCode != null and indicatorCode != ''\"> AND indicator_code = #{indicatorCode} </if>",
            "  <if test=\"indicatorName != null and indicatorName != ''\"> AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%') </if>",
            "  <if test=\"categoryId != null\"> AND category_id = #{categoryId} </if>",
            "</where>",
            "ORDER BY indicator_id DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<ExaminationIndicator> list(@Param("indicatorCode") String indicatorCode,
                                    @Param("indicatorName") String indicatorName,
                                    @Param("categoryId") Long categoryId,
                                    @Param("limit") int limit,
                                    @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM examination_indicator",
            "<where>",
            "  <if test=\"indicatorCode != null and indicatorCode != ''\"> AND indicator_code = #{indicatorCode} </if>",
            "  <if test=\"indicatorName != null and indicatorName != ''\"> AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%') </if>",
            "  <if test=\"categoryId != null\"> AND category_id = #{categoryId} </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("indicatorCode") String indicatorCode,
               @Param("indicatorName") String indicatorName,
               @Param("categoryId") Long categoryId);
}